package com.sunshine.student_register.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.room.Database;

import com.google.android.material.card.MaterialCardView;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.sunshine.student_register.R;
import com.sunshine.student_register.database.register.RegisterEntity;

import java.util.ArrayList;
import java.util.Date;

/**
 * @author sunshine
 * @date 2020/5/30
 * 回收布局适配器
 */
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerViewHolder> {

    private ArrayList<RegisterEntity> registerEntityArrayList;
    private Context context;

    public RecyclerAdapter(ArrayList<RegisterEntity> registerEntityArrayList) {
        this.registerEntityArrayList = registerEntityArrayList;
    }

    @NonNull
    @Override
    public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        this.context = parent.getContext();
        return new RecyclerViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_register, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerViewHolder holder, int position) {
        final RegisterEntity entity = registerEntityArrayList.get(position);
        final Date date = new Date();
        date.setTime(entity.getTime());
        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(context);
                builder.setTitle("详细信息");
                builder.setMessage("签到ID：" + entity.getId() +
                        "\n学生ID：" + entity.getStudentId() +
                        "\n签到时间" + date);
                builder.create().show();
            }
        });
        holder.registerId.setText(String.valueOf(entity.getId()));
        holder.name.setText("学生ID：" + entity.getStudentId());
        holder.time.setText("签到时间：" + date);
    }

    @Override
    public int getItemCount() {
        return registerEntityArrayList.size();
    }

    class RecyclerViewHolder extends RecyclerView.ViewHolder{

        MaterialCardView cardView;
        TextView registerId, name, time;

        public RecyclerViewHolder(@NonNull View itemView) {
            super(itemView);

            cardView = itemView.findViewById(R.id.cardview);
            registerId = itemView.findViewById(R.id.textView_registerid);
            name = itemView.findViewById(R.id.textView_name);
            time = itemView.findViewById(R.id.textView_time);
        }
    }
}
